package com.bw.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.bw.config.Result;
import com.bw.domain.TraceTask;
import com.bw.domain.TraceTransport;
import com.bw.domain.TraceUserCrops;
import com.bw.service.TraceTaskService;
import com.bw.service.TraceTransportService;
import com.bw.service.TraceUserCropsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 王钰--WY
 * @create 2023-08-24 10:43
 */
@RestController
@RequestMapping("/product")
public class ProductController {
    @Autowired
    TraceTransportService productService;
    @Autowired
    TraceTaskService traceTaskService;
    @Autowired
    TraceUserCropsService traceUserCropsService;
    @GetMapping("/queryList/{deptId}")
    public Result<List<TraceTransport>> queryList(@PathVariable("deptId")Integer deptId){
        List<TraceTransport> list=productService.queryList(deptId);
        return Result.ok(list);
    }
    @GetMapping("/queryTaskList")
    public Result<List<TraceTask>> queryTaskList(){
        List<TraceTask> list = traceTaskService.list();
        return Result.ok(list);
    }
    @PostMapping("/addTransport")
    public Result<Boolean> addTransport(@RequestBody TraceTransport traceTransport){
        traceUserCropsService.updateByCropsId(traceTransport.getCropsId());
        boolean b = productService.save(traceTransport);
        return Result.ok(b);
    }

    /**
     * 出库
     * @param cropsId
     * @return
     */
    @GetMapping("/productOutFactory/{cropsId}")
    public Result<Boolean> productOutFactory(@PathVariable("cropsId") String cropsId){
        Boolean b=traceUserCropsService.productOutFactory(cropsId);
        return Result.ok(b);
    }

    @PostMapping("/addTask")
    public Result<Boolean> addTask(@RequestBody TraceTask traceTask){
        boolean b = traceTaskService.save(traceTask);
        return Result.ok(b);
    }

    @PostMapping("/updateMachingStatus")
    public Result<Boolean> updateMachingStatus(@RequestBody TraceUserCrops traceUserCrops){
        LambdaQueryWrapper<TraceUserCrops> wrapper = new LambdaQueryWrapper<TraceUserCrops>()
                .eq(TraceUserCrops::getCropsId,traceUserCrops.getCropsId());
        boolean b = traceUserCropsService.update(traceUserCrops,wrapper);
        return Result.ok(b);
    }

    @GetMapping("/queryTaskByCropsId/{cropsId}")
    public Result<TraceTask> queryTaskByCropsId(@PathVariable("cropsId") String cropsId){
        TraceTask task = traceTaskService.getOne(new LambdaQueryWrapper<TraceTask>().eq(TraceTask::getCropsId, cropsId));
        return Result.ok(task);
    }
    @GetMapping("/updateProductWriteStatus")
    public Result<Boolean> updateProductWriteStatus(String cropsId){
        LambdaQueryWrapper<TraceUserCrops> wrapper = new LambdaQueryWrapper<TraceUserCrops>()
                .eq(TraceUserCrops::getCropsId, cropsId);
        TraceUserCrops userCrops = traceUserCropsService.getOne(wrapper);
        userCrops.setProductWriteStatus(1);
        boolean b = traceUserCropsService.update(userCrops, wrapper);
        return Result.ok(b);
    }
}
